﻿
/*============== simpleIRC - http://matthias.van-eeghem.com/ ==============*
 *                                                                         *
 *  File: Program.cs                                                 *
 *  Project: ConsoleClient                                                 *
 *  Author(s): Matthias Van Eeghem <matthias@van-eeghem.com>               *
 *                                                                         *
 * ========================================================================*
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
===========================================================================*/

using System;
using simpleIRC;

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*************************************************************");
            Console.WriteLine("* Starting ConsoleClient based on the simpleIRC framework.. *");
            Console.WriteLine("* Written by Matthias Van Eeghem.                           *");

            // Initialise the Core class
            Core.Initialise();

            // This class will listen to all events called from the Server class
            EventListener Listener = new EventListener();

            Console.WriteLine("* Succesfully started the ConsoleClient.                    *");
            Console.WriteLine("*************************************************************");
            Console.WriteLine();

            // Prompt the user to enter a server
            Console.WriteLine("* Please enter a hostname to connect to:");
            string Hostname = Console.ReadLine();
            Console.WriteLine("* Connecting to " + Hostname + "...");
            Console.WriteLine();

            // Create a new Server object
            Server Server = new Server(Hostname, 6667, "irc_client0", "irc_client0", "irc_client1", "Using simpleIRC");
            Server.Connect();

            // This variable holds the current channel index the user is talking in
            int CurrentChannelIndex = -1;

            // We'll check for any commands entered by the user in this infinite loop
            while(true)
            {
                string CommandLine = Console.ReadLine();
                if(CommandLine == null) continue;

                string[] Parameters = CommandLine.Split(new Char[] { ' ' });

                // The join command, the first parameter is the channel name, and the second optional parameter is the password
                if(CommandLine.StartsWith("join"))
                {
                    if (Parameters.Length > 1)
                    {
                        string Password = (Parameters.Length > 2) ? Parameters[2] : "";
                        new Channel(Server.Index, Parameters[1], Password).Join();

                        // Set the CurrentChannelIndex to that of the channel we just created
                        CurrentChannelIndex = Core.GetChannelIndex(Parameters[1], Server.Index);
                    }
                }

                // This command allows a user to switch from one channel to another to chat in it
                else if (CommandLine.StartsWith("switch"))
                {
                    if (Parameters.Length > 1)
                    {
                        Console.WriteLine("* Switched from " + Core.ChannelList[CurrentChannelIndex].Name + " to " + Parameters[1]);

                        // We simply set the CurrentChannelIndex to the new channel index
                        CurrentChannelIndex = Core.GetChannelIndex(Parameters[1], Server.Index);
                    }
                }

                // This command disconnects the user from the server
                else if (CommandLine.StartsWith("disconnect"))
                {
                    Console.WriteLine("* Disconnecting from " + Hostname);
                    Server.Disconnect();
                }

                else
                {
                    // Check if we can send any data to a valid channel
                    if(CurrentChannelIndex != -1)
                    {
                        Server.SendData("PRIVMSG " + Core.ChannelList[CurrentChannelIndex].Name + " :" + CommandLine);
                    }
                }
            }
        }
    }
}
